import { defineStore } from 'pinia';
import { cardApi } from '@/services/api';

export const useCardStore = defineStore('card', {
  state: () => ({
    cards: [],
    currentCard: null,
    relatedCards: [],
    chapters: [],
    tags: [],
    loading: false,
    error: null,
    filters: {
      chapter: null,
      tag: null,
      difficulty: null,
      search: '',
    },
    pagination: {
      page: 1,
      limit: 10,
      total: 0,
    },
  }),
  
  getters: {
    getCards: (state) => state.cards,
    getCurrentCard: (state) => state.currentCard,
    getRelatedCards: (state) => state.relatedCards,
    getChapters: (state) => state.chapters,
    getTags: (state) => state.tags,
    isLoading: (state) => state.loading,
    getError: (state) => state.error,
    getFilters: (state) => state.filters,
    getPagination: (state) => state.pagination,
  },
  
  actions: {
    // 获取卡片列表
    async fetchCards() {
      this.loading = true;
      this.error = null;
      
      try {
        const params = {
          page: this.pagination.page,
          limit: this.pagination.limit,
          ...this.filters,
        };
        
        const response = await cardApi.getCards(params);
        this.cards = response.data.cards;
        this.pagination.total = response.data.total;
        
        return { success: true, cards: this.cards, total: response.data.total };
      } catch (error) {
        this.error = error.response?.data?.message || '获取卡片失败';
        return { success: false, error: this.error };
      } finally {
        this.loading = false;
      }
    },
    
    // 获取单个卡片详情
    async fetchCard(cardId) {
      this.loading = true;
      this.error = null;
      
      try {
        const response = await cardApi.getCardById(cardId);
        this.currentCard = response.data;
        
        return { success: true, card: this.currentCard };
      } catch (error) {
        this.error = error.response?.data?.message || '获取卡片详情失败';
        return { success: false, error: this.error };
      } finally {
        this.loading = false;
      }
    },
    
    // 更新卡片
    async updateCard(cardId, cardData) {
      this.loading = true;
      this.error = null;
      
      try {
        const response = await cardApi.updateCard(cardId, cardData);
        
        // 更新当前卡片
        if (this.currentCard && this.currentCard._id === cardId) {
          this.currentCard = response.data;
        }
        
        // 更新卡片列表中的卡片
        const index = this.cards.findIndex(card => card._id === cardId);
        if (index !== -1) {
          this.cards[index] = response.data;
        }
        
        return { success: true, card: response.data };
      } catch (error) {
        this.error = error.response?.data?.message || '更新卡片失败';
        return { success: false, error: this.error };
      } finally {
        this.loading = false;
      }
    },
    
    // 获取相关卡片
    async fetchRelatedCards(cardId) {
      this.loading = true;
      this.error = null;
      
      try {
        const response = await cardApi.getRelatedCards(cardId);
        this.relatedCards = response.data;
        
        return { success: true, relatedCards: this.relatedCards };
      } catch (error) {
        this.error = error.response?.data?.message || '获取相关卡片失败';
        return { success: false, error: this.error };
      } finally {
        this.loading = false;
      }
    },
    
    // 获取所有章节
    async fetchChapters() {
      this.loading = true;
      this.error = null;
      
      try {
        const response = await cardApi.getChapters();
        this.chapters = response.data;
        
        return { success: true, chapters: this.chapters };
      } catch (error) {
        this.error = error.response?.data?.message || '获取章节失败';
        return { success: false, error: this.error };
      } finally {
        this.loading = false;
      }
    },
    
    // 获取所有标签
    async fetchTags() {
      this.loading = true;
      this.error = null;
      
      try {
        const response = await cardApi.getTags();
        this.tags = response.data;
        
        return { success: true, tags: this.tags };
      } catch (error) {
        this.error = error.response?.data?.message || '获取标签失败';
        return { success: false, error: this.error };
      } finally {
        this.loading = false;
      }
    },
    
    // 设置过滤器
    setFilters(filters) {
      this.filters = { ...this.filters, ...filters };
      this.pagination.page = 1; // 重置页码
      return this.fetchCards();
    },
    
    // 设置分页
    setPagination(pagination) {
      this.pagination = { ...this.pagination, ...pagination };
      return this.fetchCards();
    },
    
    // 重置状态
    resetState() {
      this.cards = [];
      this.currentCard = null;
      this.relatedCards = [];
      this.chapters = [];
      this.tags = [];
      this.loading = false;
      this.error = null;
      this.filters = {
        chapter: null,
        tag: null,
        difficulty: null,
        search: '',
      };
      this.pagination = {
        page: 1,
        limit: 10,
        total: 0,
      };
    },
  },
}); 